package com.tl.bootmeter.service.impl;

import com.tl.bootmeter.mapper.UserMapper;
import com.tl.bootmeter.pojo.District;
import com.tl.bootmeter.pojo.DistrictStationResult;
import com.tl.bootmeter.pojo.Station;
import com.tl.bootmeter.pojo.User;
import com.tl.bootmeter.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    /**
     * 登录
     * @param uname	用户名
     * @param upass	密码
     * @return	用户信息
     */
    @Override
    public User checkLogin(String uname, String upass) {
        User suser = userMapper.selectUser(uname,upass);
        if(suser.getUlevel().length() == 3){
            //如果用户等级的大小为3那么该用户的权限是站的，应当执行站的sql语句
            DistrictStationResult ds = userMapper.selectUserByEvelDiSR(suser.getUlevel());
            System.out.println("站权限查询出来的信息"+ds);
            //将查询出来的信息封装到user中
            //创建一个list
            List<DistrictStationResult> dl = new ArrayList<>();
            dl.add(ds);
            System.out.println("封装到list后的信息"+dl);
            suser.setDsmap(dl);
            System.out.println("最后将信息封装到user中"+suser);
            //最后将user返回
            return suser;
        }
        else if (suser.getUlevel().equals("0")) {
            //如果用户等级为0的话说明是最大权限可以看见所有的管区和站区（如何将查询出来的管区和战区关联起来呢）
            //查询出所有的管区信息
            List<District> districtAllList = userMapper.selectAllDistrict();
            System.out.println("所有管区信息的list长度="+districtAllList.size());
            //创建一个DistrictStationResult集合
            List<DistrictStationResult> dsrList = new ArrayList<>();
            for(int i = 0; i <districtAllList.size(); i++){
                System.out.println(districtAllList.get(i));
                //创建一个DistrictStationResult对象
                DistrictStationResult dsr = new DistrictStationResult();
                dsr.setDistrict(districtAllList.get(i));
                System.out.println(dsr);
                //根据管区id 查询 这个管区下有多少站
                List<Station> st = userMapper.selectByDistrictIdStation(dsr.getDistrict().getDistrict_id());
                for (int j = 0; j < st.size(); j++) {
                    //将查询出来的站集合封装到DistrictStationResult对象中
                    dsr.setStation(st);
                }
                //最后将完全封装好的DistrictStationResult放入集合中
                dsrList.add(dsr);
            }
            //将权限全部封装到集合中后将这个集合封装到user内
            suser.setDsmap(dsrList);
            System.out.println("查看最后user对象中的权限属性是否有全部的管区信息="+suser.getDsmap());
            return suser;
        }
        else {
            //如果长度为1的话就说明用户的权限是管区的权限
            District dis = userMapper.selectUserByEvelDistrict(suser.getUlevel());
            System.out.println("管区权限查询出来的信息"+dis);
            //将管区信息封装到DistrictStationResult中
            //创建一个DistrictStationResult对象
            DistrictStationResult dt = new DistrictStationResult();
            dt.setDistrict(dis);
            System.out.println("将管区信息封装到对象DistrictStationResult中="+dt);
            //根据管区id查询这个管区下一共有多少站
            List<Station> lsta = userMapper.selectByDistrictIdStation(dis.getDistrict_id());
            //将查询出来的信息封装到DistrictStationResult中
            dt.setStation(lsta);
            System.out.println("将战区信息封装到对象DistrictStationResult中="+dt);
            //创建一个list对象
            List<DistrictStationResult> dlist = new ArrayList<>();
            dlist.add(dt);
            //将list封装到user中
            suser.setDsmap(dlist);
            System.out.println("将所有管区战区权限封装到user中="+suser);
            return suser;
        }
    }
    /**
     * 用户注册
     * @param uname
     * @param upass
     * @param unickName
     * @param ugender
     * @param uphone
     * @param district_id
     * @param station_id
     * @return 1成功 0 失败
     */
    @Override
    public int insertUser(String uname, String upass, String unickName, String ugender, String uphone, int district_id, int station_id) {
        if(station_id != 0){
            //获取当前时间戳来当用户id
            Integer time = (int)(System.currentTimeMillis()/1000);
            User newUser = new User();
            newUser.setUid(time);
            newUser.setUname(uname);
            newUser.setUpass(upass);
            newUser.setUnickName(unickName);
            newUser.setUgender(ugender);
            newUser.setUphone(uphone);
            newUser.setUlevel(String.valueOf(district_id)+"-"+String.valueOf(station_id));
            //将管区id和站id拼接成用户等级
            System.out.println(String.valueOf(district_id)+"-"+String.valueOf(station_id));
            int resultUser = userMapper.insertUser(newUser);
            return resultUser;
        }else{
            //获取当前时间戳来当用户id
            Integer time = (int)(System.currentTimeMillis()/1000);
            User newUser = new User();
            newUser.setUid(time);
            newUser.setUname(uname);
            newUser.setUpass(upass);
            newUser.setUnickName(unickName);
            newUser.setUgender(ugender);
            newUser.setUphone(uphone);
            newUser.setUlevel(String.valueOf(district_id));
            //将管区id和站id拼接成用户等级
            System.out.println(String.valueOf(district_id));
            int resultUser = userMapper.insertUser(newUser);
            return resultUser;
        }
    }

    @Override
    public boolean selectUserByuname(String uname) {
        User userUname = userMapper.selectUserByuname(uname);
        if(userUname == null){
            return false;
        }else{
            return true;
        }
    }
}
